package com.servlet;

import com.dao.Impl.StorageinfoDaoImpl;
import com.dao.StorageinfoDao;
import com.entity.PageBean;
import com.entity.Storageinfo;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author
 */
@WebServlet(value = "/storgaeinfoServlet/*")
public class StorgaeinfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        String target = "";
        switch (method){
            case "insertStorageinfo":
                target=insertStorageinfo(request,response);
                break;
            case "findAllStorageinfo":
                target=findAllStorageinfo(request,response);
                break;
            default:break;
        }
        if (target!=""){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }

    private String findAllStorageinfo(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Map> page=new PageBean<>();
        TransBean.populate(page,request.getParameterMap());
        StorageinfoDao dao=new StorageinfoDaoImpl();
        dao.pageFindStoraginfo(page);
        MyUtil.jsonWriter(response,page);
        return null;
    }

    protected String insertStorageinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Storageinfo si=new Storageinfo();
        TransBean.populate(si,request.getParameterMap());
        StorageinfoDao dao=new StorageinfoDaoImpl();
        int i = dao.insertStorageinfo(si);
        MyUtil.jsonWriter(response,i);
        return null;
    }
}
